﻿Imports System
Imports System.Drawing
Imports System.Windows.Forms

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class AutoExpandingMessageFilter
        Inherits WorkflowDesignerMessageFilter
        ' Methods
        Friend Sub New()
        End Sub

        Private Sub OnAutoExpand(ByVal sender As Object, ByVal eventArgs As EventArgs)
            If (Not Me.autoExpandableDesigner Is Nothing) Then
                Me.autoExpandableDesigner.Expanded = True
                MyBase.ParentView.PerformLayout(True)
            End If
            Me.SetAutoExpandableDesigner(Nothing)
        End Sub

        Protected Overrides Function OnDragDrop(ByVal eventArgs As DragEventArgs) As Boolean
            Me.SetAutoExpandableDesigner(Nothing)
            Return False
        End Function

        Protected Overrides Function OnDragEnter(ByVal eventArgs As DragEventArgs) As Boolean
            Dim parentView As WorkflowView = MyBase.ParentView
            If parentView.IsClientPointInActiveLayout(parentView.PointToClient(New Point(eventArgs.X, eventArgs.Y))) Then
                Me.SetAutoExpandableDesigner([TryCast](parentView.MessageHitTestContext.AssociatedDesigner,CompositeActivityDesigner))
            Else
                Me.SetAutoExpandableDesigner(Nothing)
            End If
            Return False
        End Function

        Protected Overrides Function OnDragLeave() As Boolean
            Me.SetAutoExpandableDesigner(Nothing)
            Return False
        End Function

        Protected Overrides Function OnDragOver(ByVal eventArgs As DragEventArgs) As Boolean
            Dim parentView As WorkflowView = MyBase.ParentView
            If parentView.IsClientPointInActiveLayout(parentView.PointToClient(New Point(eventArgs.X, eventArgs.Y))) Then
                Me.SetAutoExpandableDesigner([TryCast](parentView.MessageHitTestContext.AssociatedDesigner,CompositeActivityDesigner))
            Else
                Me.SetAutoExpandableDesigner(Nothing)
            End If
            Return False
        End Function

        Private Sub SetAutoExpandableDesigner(ByVal value As CompositeActivityDesigner)
            If (Not Me.autoExpandableDesigner Is value) Then
                If (((value Is Nothing) OrElse value.Expanded) OrElse Not value.CanExpandCollapse) Then
                    Me.autoExpandableDesigner = Nothing
                    If (Not Me.autoExpandEventHandler Is Nothing) Then
                        WorkflowTimer.Default.Unsubscribe(Me.autoExpandEventHandler)
                        Me.autoExpandEventHandler = Nothing
                    End If
                Else
                    Me.autoExpandableDesigner = value
                    If (Me.autoExpandEventHandler Is Nothing) Then
                        Me.autoExpandEventHandler = New EventHandler(AddressOf Me.OnAutoExpand)
                        WorkflowTimer.Default.Subscribe(500, Me.autoExpandEventHandler)
                    End If
                End If
            End If
        End Sub


        ' Fields
        Private autoExpandableDesigner As CompositeActivityDesigner
        Private autoExpandEventHandler As EventHandler
    End Class
End Namespace

